#include <cmath>
#include <cstring>
#include <iostream>

/**
 * NowCoder KY58: Repeater
 */

int powi(int a, int k) {
    int result = 1;
    while (k != 0) {
        if (k % 2 != 0) result *= a;
        a *= a;
        k /= 2;
    }
    return result;
}

char pat[5][5];
char buf[3000][3000];

void draw(int rBegin, int cBegin, int size, int len, int level) {
    // Directly draw the pattern at first level
    if (level == 1) {
        int rEnd = rBegin + size;
        int cEnd = cBegin + size;
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                int row = i + rBegin, col = j + cBegin;
                buf[row][col] = pat[i][j];
            }
        }
        return;
    }

    // Split into several sub-patterns
    int subLen = len / size;
    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            if (pat[i][j] == ' ') continue;
            int subRowBegin = rBegin + i * subLen;
            int subColBegin = cBegin + j * subLen;
            draw(subRowBegin, subColBegin, size, subLen, level - 1);
        }
    }
}

int main() {
    int size;
    while (scanf("%d", &size) != EOF) {
        // Read data
        if (size == 0) break;
        std::string line;
        std::getline(std::cin, line);
        for (int i = 0; i < size; i++) {
            std::getline(std::cin, line);
            for (int j = 0; j < size; j++) pat[i][j] = line[j];
        }
        int level;
        scanf("%d", &level);

        // Draw
        int len = powi(size, level);
        for (int i = 0; i < len; i++) memset(buf[i], ' ', len);
        draw(0, 0, size, len, level);

        // Plot result
        for (int i = 0; i < len; i++) {
            for (int j = 0; j < len; j++) printf("%c", buf[i][j]);
            printf("\n");
        }
    }
}